package com.glsc.ngateway.opmanage.service.deposit;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.oracle.xinyizj.FsAccBankInfo;
import com.glsc.ngateway.common.base.domain.oracle.xinyizj.FsBankBal;
import com.glsc.ngateway.common.base.repo.oracle.xinyizj.FsBankBalRepository;
import com.glsc.ngateway.opmanage.utils.SpecificationUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@Service
public class FsBankBalService {


    @Resource
    private FsBankBalRepository fsBankBalRepository;


    @Resource
    private FsAccBankInfoService fsAccBankInfoService;

    /**
     * 根据银行账号查询账号余额信息
     *
     * @param bankAcc
     * @return
     */
    public FsBankBal findByBankAcc(String bankAcc) {

        FsAccBankInfo accBankInfo = fsAccBankInfoService.findByBankAcc(bankAcc);
        if (Objects.isNull(accBankInfo)) {
            return null;
        }

        List<FsBankBal> bankBals = fsBankBalRepository.findAll(Example.of(FsBankBal.builder().bankAccId(accBankInfo.getBankAccId()).build()));
        if (CollectionUtils.isEmpty(bankBals)) {
            return null;
        }

        return bankBals.get(0);
    }


    /**
     * 分页查找
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<FsBankBal> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "bankAcc";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<FsBankBal> page = fsBankBalRepository.findAll(SpecificationUtil.buildSpecification(param, FsBankBal.class), pageable);

        return page;
    }

    /**
     * 查询所有
     *
     * @return
     */
    public List<FsBankBal> listAll() {
        List<FsBankBal> list = fsBankBalRepository.findAll(Example.of(FsBankBal.builder().build()));
        return list;
    }


    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public FsBankBal findById(Long id) {
        Optional<FsBankBal> optional = fsBankBalRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }


}
